package com.samsung.service.impl.vote;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.samsung.common.util.BaseUtil;
import com.samsung.dao.vote.OptionMapper;
import com.samsung.dao.vote.TopicMapper;
import com.samsung.dao.vote.VoteDataMapper;
import com.samsung.dto.vote.CreateVoteDto;
import com.samsung.model.user.User;
import com.samsung.model.vote.VoteOption;
import com.samsung.model.vote.VoteTopic;
import com.samsung.service.vote.CreateVoteService;

@Service("CreateVoteService")
public class CreateVoteServiceImpl implements CreateVoteService {

	@Resource
	private TopicMapper topicMapper;
	@Resource
	private OptionMapper optionMapper;
	
	@Resource
	private VoteDataMapper voteDataMapper;

	/**
	 * @title checkTopicInfo
	 * @Description 新建投票信息topic的校验 
	 * @param topic 新建投票信息的model
	 * @param createVoteDto 新建投票信息的中间model
	 * @return Map 校验结果集
	 */
	@Override
	public Map<String, Object> checkTopicInfo(VoteTopic topic,CreateVoteDto createVoteDto) {
		Map<String,Object> model = new HashMap<String,Object>();
		if (BaseUtil.isEmpty(topic.getTitle())) {
			model.put("message", "标题不能为空");
			model.put("flag", "failure");
			return model;
		}
		if (topic.getTitle().length() > 140) {
			model.put("message", "标题长度不大于140字符");
			model.put("flag", "failure");
			return model;
		}
		//如果是更新，原标题与现标题一致，则不需要校验标题
		if( BaseUtil.isNotEmpty(topic.getOriTitle()) ){//for update
			if( !(topic.getOriTitle().equals(topic.getTitle())) ){
				if (topicMapper.checkTitle(topic.getTitle()).size() > 0) {
					model.put("message", "标题已经存在，请重新命名");
					model.put("flag", "failure");
					return model;
				} 
			}
		}else{
			if (BaseUtil.isEmpty(topic.getTid()) && topicMapper.checkTitle(topic.getTitle()).size() > 0) {
				model.put("message", "标题已经存在，请重新命名");
				model.put("flag", "failure");
				return model;
			} 
		}
		if (topic.getType() == null) {
			model.put("message", "投票类型为必选项");
			model.put("flag", "failure");
			return model;
		} else {
			if (topic.getType()!=0 && topic.getType() != 1) {
				model.put("message", "投票类型的类型错误");
				model.put("flag", "failure");
				return model;
			}
		}

		if (topic.getScope()== null) {
			model.put("message", "投票隐私为必选项");
			model.put("flag", "failure");
			return model;
		} else {
			if (topic.getScope()!=0 && topic.getScope() != 1) {
				model.put("message", "投票隐私的类型错误");
				model.put("flag", "failure");
				return model;
			}
		}
		if (BaseUtil.isEmpty(createVoteDto.getEndTime())) {
			model.put("message", "投票截止时间不能为空");
			model.put("flag", "failure");
			return model;
		}
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date deadLineDate = null;
		try {
			deadLineDate = sdf.parse(createVoteDto.getEndTime()+" 23:59:59");
		} catch (ParseException e) {
			model.put("message", "投票截止时间日期格式错误");
			model.put("flag", "failure");
			return model;
		}
		topic.setDeadline(deadLineDate);
		if (topic.getDeadline().before(new Date())) {
			model.put("message", "投票截止时间不能为系统时间之前");
			model.put("flag", "failure");
			return model;
		}
		
		if( createVoteDto.getContent1() != null && createVoteDto.getContent1() == "" ){
			model.put("message", "投票选项内容不能为空哦");
			model.put("flag", "failure");
			return model;
		}
		if( createVoteDto.getContent2() != null && createVoteDto.getContent2() == "" ){
			model.put("message", "投票选项内容不能为空哦");
			model.put("flag", "failure");
			return model;
		}
		if( createVoteDto.getContent3() != null && createVoteDto.getContent3() == "" ){
			model.put("message", "投票选项内容不能为空哦");
			model.put("flag", "failure");
			return model;
		}
		if( createVoteDto.getContent4() != null && createVoteDto.getContent5() == "" ){
			model.put("message", "投票选项内容不能为空哦");
			model.put("flag", "failure");
			return model;
		}
		if( createVoteDto.getContent6() != null && createVoteDto.getContent6() == "" ){
			model.put("message", "投票选项内容不能为空哦");
			model.put("flag", "failure");
			return model;
		}
		if( createVoteDto.getContent7() != null && createVoteDto.getContent7() == "" ){
			model.put("message", "投票选项内容不能为空哦");
			model.put("flag", "failure");
			return model;
		}
		if( createVoteDto.getContent8() != null && createVoteDto.getContent8() == "" ){
			model.put("message", "投票选项内容不能为空哦");
			model.put("flag", "failure");
			return model;
		}
		if( createVoteDto.getContent9() != null && createVoteDto.getContent9() == "" ){
			model.put("message", "投票选项内容不能为空哦");
			model.put("flag", "failure");
			return model;
		}
		return model;
	}

	/**
	 * @title checkTopicInfo
	 * @Description 新建投票信息入库Topic表 and update
	 * @param topic 新建投票信息的model
	 */
	@Override
	public Map<String, Object> saveTopic(VoteTopic topic,CreateVoteDto createVoteDto, User user) {
		Map<String,Object> model = new HashMap<String,Object>();
		model = this.checkTopicInfo(topic, createVoteDto);
		if( model.size() == 0 ){
			//如果是更新投票详情，则需要原始title，根据原始title来set topic对象
			if( topic.getOriTitle() == null || "".equals(topic.getOriTitle()) ){
				topic.setUid(user.getUid());
//				topic.setUid("dong");
				topic.setTid(BaseUtil.getUUID());
				topic.setCreateTime(new Date());
				topic.setAdvert("upload/3.png");
			}else{
				topic.setCreateTime(new Date());
				topic.setAdvert("upload/3.png");
			}
			List<VoteOption> optionList = new ArrayList<VoteOption>();
			optionList = this.setOptionInfo(topic,createVoteDto);
			model = this.checkOptionInfo(optionList);
			if( model.size() == 0 ){
				//如果是新建就插入标题，更新就更新标题
				if( topic.getOriTitle() == null || "".equals(topic.getOriTitle()) ){
					topicMapper.insert(topic);
				}else{
					topicMapper.updateByPrimaryKeySelective(topic);
				}
				//更新需要删掉原有的所有相应的选项数据
				if( topic.getOriTitle() != null && !"".equals(topic.getOriTitle()) ){
					voteDataMapper.deleteVoteOptoionById(topic.getTid());
				}
				for (VoteOption option :optionList) {
					optionMapper.insert(option);
				}
			}else{
				return model;
			}
		}else{
			return model;
		}
		model.put("flag", "success");
		model.put("message", "投票创建成功");
		return model;
	}

	/**
	 * @title checkTopicInfo
	 * @Description 新建投票信息入库option表
	 * @param optionList 新建投票信息model的List
	 */
	@Override
	public void saveOption(List<VoteOption> optionList) {
		for (VoteOption option :optionList) {
			optionMapper.insert(option);
		}
	}

	/**
	 * @title checkTopicInfo
	 * @Description 新建投票信息option的校验
	 * @param topic 新建投票信息model的List
	 * @return Map 校验结果集
	 */
	@Override
	public Map<String, Object> checkOptionInfo(List<VoteOption> optionList) {
		Map<String,Object> model = new HashMap<String,Object>();
		for (int i=0; i<optionList.size(); i++){
			if( optionList.get(i).getContent().length() <= 0 ){
				model.put("message", "新建投票必须有投票选项");
				model.put("flag", "failure");
				return model;
			}
			if (optionList.get(i).getContent().length() > 255) {
				model.put("message", "投票选项" + i + "长度不大于255");
				model.put("flag", "failure");
				return model;
			}
			
		}
		for (int i=0; i<optionList.size(); i++){
			for (int j=1+i; j<optionList.size(); j++) {
				if (optionList.get(i).getContent().equals(optionList.get(j).getContent())) {
					model.put("message", "投票选项有重复的，请修改");
					model.put("flag", "failure");
					return model;
				}
			}
		}
		return model;
	}

	/**
	 * @title checkTopicInfo
	 * @Description 新建投票model option信息存入
	 * @param topic 新建投票信息的model
	 * @param CreateVoteDto 新建投票信息的中间model
	 * @return List option List结果集
	 */
	@Override
	public List<VoteOption> setOptionInfo(VoteTopic topic, CreateVoteDto createVoteDto) {
		List<VoteOption> optionList = new ArrayList<VoteOption>();
		Date createTime = new Date();
		String tid = topic.getTid();
		List<String> content = new ArrayList<String>();
		if (BaseUtil.isNotEmpty(createVoteDto.getContent1())) {
			content.add(createVoteDto.getContent1());
		}
		if (BaseUtil.isNotEmpty(createVoteDto.getContent2())) {
			content.add(createVoteDto.getContent2());
		}
		if (BaseUtil.isNotEmpty(createVoteDto.getContent3())) {
			content.add(createVoteDto.getContent3());
		}
		if (BaseUtil.isNotEmpty(createVoteDto.getContent4())) {
			content.add(createVoteDto.getContent4());
		}
		if (BaseUtil.isNotEmpty(createVoteDto.getContent5())) {
			content.add(createVoteDto.getContent5());
		}
		if (BaseUtil.isNotEmpty(createVoteDto.getContent6())) {
			content.add(createVoteDto.getContent6());
		}
		if (BaseUtil.isNotEmpty(createVoteDto.getContent7())) {
			content.add(createVoteDto.getContent7());
		}
		if (BaseUtil.isNotEmpty(createVoteDto.getContent8())) {
			content.add(createVoteDto.getContent8());
		}
		if (BaseUtil.isNotEmpty(createVoteDto.getContent9())) {
			content.add(createVoteDto.getContent9());
		}
		if (BaseUtil.isNotEmpty(createVoteDto.getContent10())) {
			content.add(createVoteDto.getContent10());
		}
		for (int j=0; j< content.size(); j++) {
			VoteOption option = new VoteOption();
			option.setContent(content.get(j));
			option.setCreateTime(createTime);
			option.setOid(BaseUtil.getUUID());
			option.setOid(BaseUtil.getUUID());
			option.setOrderNum(j+1);
			option.setTid(tid);
			optionList.add(option);
		}
		return optionList;
	}

	/**
	 * @title checkTitle
	 * @Description 新建投票信息标题的异步校验
	 * @param title 新建投票信息的标题
	 * @return Map 校验结果集
	 */
	@Override
	public Map<String, Object> checkTitle(String title) {
		Map<String,Object> model = new HashMap<String,Object>();
		if (BaseUtil.isEmpty(title.trim())) {
			model.put("message", "请输入标题");
			model.put("flag", "failure");
			return model;
		}
		if (topicMapper.checkTitle(title).size() > 0) {
			model.put("message", "标题已经存在，请重新命名");
			model.put("flag", "failure");
			return model;
		} 
		model.put("flag", "success");
		return model;
	}
}
